<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <!--      <el-form-item label="投诉分类:" prop="type">-->
      <!--        <el-select v-model="queryParams.type" placeholder="请选择" clearable size="mini">-->
      <!--          <el-option-->
      <!--            v-for="item in categoryList"-->
      <!--            :key="item.id"-->
      <!--            :label="item.name"-->
      <!--            :value="item.id"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item label="联系姓名:" prop="name">
        <el-input
          size="mini"
          v-model="queryParams.name"
          placeholder="请输入联系姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话:" prop="mobile">
        <el-input
          size="mini"
          v-model="queryParams.mobile"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系地址:" prop="address">
        <el-input
          size="mini"
          v-model="queryParams.address"
          placeholder="请输入联系地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投诉标题:" prop="title">
        <el-input
          size="mini"
          v-model="queryParams.title"
          placeholder="请输入投诉标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投诉内容:" prop="content">
        <el-input
          size="mini"
          v-model="queryParams.content"
          placeholder="请输入投诉内容"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:complaint:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column label="投诉编号" align="center" prop="id"/>
      <el-table-column label="主体单位名称" align="center" prop="enterprise"/>
      <el-table-column label="联系姓名" align="center" prop="name"/>
      <el-table-column label="联系电话" align="center" prop="mobile"/>
      <el-table-column label="联系地址" align="center" prop="address" :show-overflow-tooltip="true"/>
      <el-table-column label="邮箱" align="center" prop="email" :show-overflow-tooltip="true"/>
      <el-table-column label="投诉类型" align="center" prop="type"/>
      <el-table-column label="投诉标题" align="center" prop="title" :show-overflow-tooltip="true"/>
      <el-table-column label="投诉内容" align="center" prop="content" width="180">
        <template slot-scope="scope">
          <el-popover
            placement="top"
            trigger="click"
            width="400"
          >
            <div
              style="display: flex; justify-content: space-between; align-items: center; word-break: break-all; min-height: 40px;">
              <span style="
              flex: 1; white-space: pre-wrap;">{{ scope.row.content }}</span>
              <el-button
                size="small"
                type="text"
                @click.stop="copyContent(scope.row.content)"
                style="margin-left: 10px;"
              >复制
              </el-button>
            </div>
            <span slot="reference" style="cursor: pointer;">
        {{ abbreviate(scope.row.content) }}
      </span>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="附件" align="center" prop="attachment">
        <template slot-scope="scope">
          <image-preview class="list-img" :src="scope.row.attachment"/>
        </template>
      </el-table-column>
      <el-table-column label="牵头部门" align="center" prop="leadDept" :show-overflow-tooltip="true"/>
      <el-table-column label="审签部门" align="center" prop="auditDept" :show-overflow-tooltip="true"/>
      <el-table-column label="流转审签类型" :show-overflow-tooltip="true" prop="circulationType" key="circulationType"
                       align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.circulation_type" :value="scope.row.circulationType"/>
        </template>
      </el-table-column>
      <el-table-column label="办理时限（工作日）" align="center" prop="timeLimit"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" class-name="small-padding fixed-width" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:complaint:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="goDetail(scope.row)"
            v-hasPermi="['system:complaint:query']"
          >详情
          </el-button>
          <el-button
            v-if="scope.row.circulationType === '0'"
            size="mini"
            type="text"
            @click="handleCirculation(scope.row,0)"
            v-hasPermi="['system:complaint:circulation']"
          >流转
          </el-button>
          <el-button
            v-if="scope.row.circulationType !== '0'"
            size="mini"
            type="text"
            @click="handleCirculation(scope.row,1)"
            v-hasPermi="['system:complaint:circulation']"
          >更新流转
          </el-button>
          <el-button
            v-if="scope.row.circulationType !== '0'"
            type="text"
            size="mini"
            @click="handleExport(scope.row)"
            v-hasPermi="['system:complaint:export']"
          >导出
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog title="详情" :visible.sync="detailOpen" width="800px" append-to-body>
      <el-form :model="detail" ref="form" label-width="100px">
        <el-row :gutter="24">
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="投诉编号: ">
                {{ detail.id }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="主体单位名称: ">
                {{ detail.enterprise }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="联系姓名: ">
                {{ detail.name }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="联系电话: ">
                {{ detail.mobile }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="联系地址: ">
                {{ detail.address }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="邮箱: ">
                {{ detail.email }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="投诉类型: ">
                {{ detail.type }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="投诉标题: ">
                {{ detail.title }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="投诉内容: ">
                {{ detail.content }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="附件: ">
                <span v-if="detail.attachment === null || detail.attachment === ''">暂无</span>
                <div class="viewer-list-img" v-else>
                  <viewer :images="detail.attachment">
                    <image-preview v-for="src in detail.attachment" :src="src" :key="src" class="detail-img"/>
                  </viewer>
                </div>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="投诉时间: ">
                {{ detail.createTime }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="牵头部门: ">
                {{ detail.leadDept }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="审签部门: ">
                {{ detail.auditDept }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="流转审签类型: ">
                <dict-tag :options="dict.type.circulation_type" :value="detail.circulationType"/>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="办理时限（工作日）: ">
                {{ detail.timeLimit }}
              </el-form-item>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

    <el-dialog :title="circulationTitle" :visible.sync="circulationOpen" width="700px" append-to-body>
      <el-form ref="circulationForm" :model="circulationForm" :rules="circulationRules" label-width="100px">
        <el-form-item label="牵头部门:" prop="leadDept">
          <el-select v-model="circulationForm.leadDept" placeholder="请选择牵头部门" clearable style="width: 100%">
            <el-option
              v-for="item in optionSelect"
              :key="item.deptName"
              :label="item.deptName"
              :value="item.deptName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="流转类型:" prop="circulationType">
          <el-radio-group v-model="circulationForm.circulationType">
            <el-radio
              v-if="dict.value !== '0'"
              v-for="dict in dict.type.circulation_type"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审签部门:" prop="auditDept">
          <el-select v-model="circulationForm.auditDept" placeholder="请选择审签部门" clearable style="width: 100%">
            <el-option
              v-for="item in optionSelect"
              :key="item.deptName"
              :label="item.deptName"
              :value="item.deptName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="办理时限:" prop="timeLimit">
          <el-input type="number" v-model="circulationForm.timeLimit" placeholder="请输入办理时限">
            <template slot="append">个工作日</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="circulationOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {pageList, remove, getInfo, categoryList, circulation} from "@/api/triones/complaint";
import {optionSelect} from '@/api/triones/overt';

export default {
  name: "index",
  dicts: ['circulation_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      dataList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        mobile: undefined,
        address: undefined,
        title: undefined,
        content: undefined,
        type: undefined
      },
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      detailOpen: false,
      detail: {},
      categoryList: [],
      circulationForm: {},
      circulationOpen: false,
      circulationRules: {
        leadDept: [
          {required: true, message: "牵头部门不能为空", trigger: "blur"}
        ],
        circulationType: [
          {required: true, message: "流转类型不能为空", trigger: "blur"}
        ],
        auditDept: [
          {required: true, message: "审签部门不能为空", trigger: "blur"}
        ],
        timeLimit: [
          {required: true, message: "办理时限不能为空", trigger: "blur"}
        ],
      },
      circulationTitle: "",
      optionSelect: []
    };
  },
  created() {
    this.getList();
    this.getCategoryList();
  },
  methods: {
    getCategoryList() {
      categoryList().then(response => {
        this.categoryList = response.data;
      });
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      pageList(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport(row) {
      const id = row.id;
      this.download('system/complaint/pdf/export/' + id, {
      }, `投诉编号-${id}.pdf`)
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const complaintIds = row.id || this.ids;
      this.$modal.confirm('是否确认删除投诉编号为"' + complaintIds + '"的数据项？').then(function () {
        return remove(complaintIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 截取字符串并加省略号 */
    abbreviate(text, length = 20) {
      return text && text.length > length ? text.substr(0, length) + '...' : text;
    },
    /** 复制内容到剪贴板 */
    copyContent(content) {
      const textarea = document.createElement('textarea');
      textarea.value = content;
      document.body.appendChild(textarea);
      textarea.select();
      document.execCommand('copy');
      document.body.removeChild(textarea);
      this.$message.success('内容已复制到剪贴板');
    },
    goDetail(row) {
      const id = row.id;
      getInfo(id).then(response => {
        this.detail = response.data;
        this.detail.attachment = response.data.attachment ? response.data.attachment.split(',') : [];
        this.detailOpen = true;
      })
    },
    /** 去流转 */
    handleCirculation(row, type) {
      this.getOptionSelect();
      this.circulationReset();
      this.circulationTitle = "流转";
      if (type === 1) {
        this.circulationTitle = "更新流转";
        getInfo(row.id).then(response => {
          this.circulationForm = response.data;
        })
      }
      this.circulationForm.id = row.id;
      this.circulationOpen = true;
    },
    circulationReset() {
      this.circulationForm = {
        id: undefined,
        leadDept: undefined,
        auditDept: '区发展改革委',
        circulationType: '1',
        timeLimit: 5
      }
      this.resetForm("circulationForm");
    },
    getOptionSelect() {
      optionSelect().then(res => {
        this.optionSelect = res.data;
      });
    },
    submitForm() {
      this.$refs["circulationForm"].validate(valid => {
        circulation(this.circulationForm).then(res => {
          this.$modal.msgSuccess("流转成功");
          this.circulationOpen = false;
          this.getList();
        })
      });
    }
  }
};
</script>
